﻿using System;
using System.Data;
using System.Xml;
using ZYCommon;
using Bifrost;
using TextEditor.TextDocument.Document;

namespace TextEditor
{
  

    //对数据库表ET_Document中记录的操作
    public class ET_Document : ZYDBRecordBase
    {
        private int intObjectType = -1;
        public string KeyPreFix = "0";//"000";
        public static string strDeptment = " ";
        private string strObjectData = " ";
        private string strObjectID = null;
        private string strObjectName = " ";
        private string strOwnerSection = " ";

        public override ZYDBRecordBase CreateNewRecord()
        {
            return new ET_Document();
        }

        //从结果集流中读取数据
        public override bool FromReader(IDataReader myReader, ZYDBConnection conn)
        {
            if ((myReader != null) && ((myReader.FieldCount == 4) || (myReader.FieldCount == 4)))
            {
                this.strObjectID = myReader.IsDBNull(0) ? "0" : myReader.GetDecimal(0).ToString ();//myReader.GetInt32(0).ToString();
                this.strObjectName = myReader.IsDBNull(1) ? " " : myReader.GetString(1);
                this.intObjectType = myReader.IsDBNull(2) ? -1 : Convert.ToInt32(myReader[2]);
                //this.strOwnerSection = myReader.IsDBNull(3) ? " " : myReader.GetString(3);
                if (myReader.FieldCount == 4)
                {
                    this.strObjectData = myReader.IsDBNull(3) ? " " : myReader.GetString(3);
                }
                return true;
            }
            return false;
        }

        //读取XML文件  直接从数据库表ET_Document中读取ObjectData列的值
        public XmlDocument GetDataXML()
        {
            XmlDocument myDoc = new XmlDocument();
            myDoc.PreserveWhitespace = true;
            myDoc.LoadXml(this.strObjectData);
            return myDoc;
        }

        //查询语句
        public static string GetSelectSQL()
        {
            return "Select ObjectID , ObjectName , ObjectType , ObjectData From ET_Document ";
        }

        public override string GetTableName()
        {
            return "ET_Document";
        }

        public override bool isKeyEnable()
        {
            return ((this.strObjectID != null) && (this.strObjectID.Length == 10));
        }

        public ZYTextDocument LoadDocument(ZYDBConnection conn)
        {
            if (conn == null)
            {
                conn = ZYDBConnection.Instance;
            }
            if (conn.ReadOneRecord(this))
            {
                ZYTextDocument NewDoc = new ZYTextDocument();
                NewDoc.DataSource.DBConn = conn;
                NewDoc.FromXML(this.GetDataXML().DocumentElement);
                return NewDoc;
            }
            return null;
        }

        public override bool SetAllocKeyCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            SEQCreator mySEQ = new SEQCreator();
            mySEQ.FixLength = 10;
            myCmd.CommandText = "Select Max( objectid )  From ET_Document";// Where OWNERSECTION='" + strDeptment + "'";
            IDataReader myReader = myCmd.ExecuteReader(CommandBehavior.SingleRow | CommandBehavior.SingleResult);
            if (myReader.Read() && !myReader.IsDBNull(0))
            {
                mySEQ.SEQ = myReader.GetString(0);
            }
            else
            {
                mySEQ.SEQ = this.KeyPreFix + "0";
            }
            myReader.Close();
            this.strObjectID = mySEQ.Create();
            return true;
        }

        public override bool SetDeleteCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Delete From ET_Document Where ObjectID = " + this.strObjectID;
            return true;
        }

        public override bool SetInsertCommnad(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Insert Into ET_Document ( objectid, ObjectName , ObjectType  , ObjectData ) Values ( ? , ? , ? ,? , ?)";
            ZYDBConnection.AddParameter(myCmd, this.strObjectID);
            ZYDBConnection.AddParameter(myCmd, this.strObjectName);
            ZYDBConnection.AddParameter(myCmd, this.intObjectType);
            ZYDBConnection.AddParameter(myCmd, strDeptment);
            ZYDBConnection.AddParameter(myCmd, this.strObjectData);
            return true;
        }

        public override bool SetKeyWord(string strKey)
        {
            this.strObjectID = strKey;
            return true;
        }

        public override bool SetSelectCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Select ObjectID , ObjectName , ObjectType   From ET_Document Order by ObjectName ";
            return true;
        }

        public override bool SetSelectCommandForOneRecord(IDbCommand myCmd, ZYDBConnection conn)
        {
            String str = this.strObjectID;
            myCmd.CommandText = "Select ObjectID , ObjectName , ObjectType  , ObjectData From ET_Document Where ObjectID =  '" + this.strObjectID + "'"; //"select * from ET_DOCUMENT Where ObjectID = ?";//
            //myCmd.Parameters.Clear();
            //ZYDBConnection.AddParameter(myCmd, this.strObjectID);
            return true;
        }

        public override DataSet GetRecordDs(IDbCommand myCmd, ZYDBConnection conn)
        {
            String str = this.strObjectID;
            
            string Sql = "Select ObjectID , ObjectName , ObjectType  , ObjectData From ET_Document Where ObjectID =  '" + this.strObjectID + "'"; //"select * from ET_DOCUMENT Where ObjectID = ?";//
            return App.GetDataSet(Sql);
            //myCmd.Parameters.Clear();
            //ZYDBConnection.AddParameter(myCmd, this.strObjectID);
            //return ds;
        }

        public static void SetSelectSEQCommand(IDbCommand myCmd)
        {
            myCmd.CommandText = "Select ObjectID From ET_Document Order by ObjectID";
        }

        public override bool SetTestExistCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "select objectid from et_Document Where objectid = '" + this.strObjectID + "'";
            return true;
        }

        public override bool SetUpdateCommnad(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Update ET_Document Set ObjectName = ? , ObjectType = ?  , ObjectData = ?  Where ObjectID = ? ";
            myCmd.Parameters.Clear();
            ZYDBConnection.AddParameter(myCmd, this.strObjectName);
            ZYDBConnection.AddParameter(myCmd, this.intObjectType);
            ZYDBConnection.AddParameter(myCmd, this.strObjectData);
            ZYDBConnection.AddParameter(myCmd, this.strObjectID);
            return true;
        }

        public string ObjectData
        {
            get
            {
                return this.strObjectData;
            }
            set
            {
                this.strObjectData = value;
            }
        }

        public string ObjectID
        {
            get
            {
                return this.strObjectID;
            }
            set
            {
                this.strObjectID = value;
            }
        }

        public string ObjectName
        {
            get
            {
                return this.strObjectName;
            }
            set
            {
                this.strObjectName = value;
            }
        }

        public int ObjectType
        {
            get
            {
                return this.intObjectType;
            }
            set
            {
                this.intObjectType = value;
            }
        }

        public string OwnerSection
        {
            get
            {
                return this.strOwnerSection;
            }
            set
            {
                this.strOwnerSection = value;
            }
        }
    }
}

